package org.apselex.providers;

/*
 * Copyright (c) 2008 Evan Williams
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apselex.model.ModelElement;
import org.apselex.model.ModelMetaData;
import org.apselex.model.ModelTag;

import apselmodel.Activator;


import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;

public class XmlTranslator {

	private static Logger logger = Logger.getLogger(XmlTranslator.class);

	public static Element transform(ModelElement element) {
		Element e = new Element("item");

		e.addAttribute(new Attribute("id", String.valueOf(element.getId())));
		e.addAttribute(new Attribute("name", element.getName()));
		e.addAttribute(new Attribute("filename", element.getFilename()));

		ModelMetaData[] meta = element.getMetadata();
		for (ModelMetaData md: meta) {
			Element tag = new Element("metadata");
			tag.addAttribute(new Attribute("name", md.getName()));
			tag.addAttribute(new Attribute("value", md.getValue()));
			e.appendChild(tag);
		}
		ModelTag[] tags = element.getTags();
		for (ModelTag t: tags) {
			Element tag = new Element("tag");
			tag.addAttribute(new Attribute("id", String.valueOf(t.getTagId())));
			tag.addAttribute(new Attribute("name", t.getTagName()));
			e.appendChild(tag);
		}
		
		return e;
	}

	public static Element transform(ModelTag root) {
		Element base = new Element("tagtree");
	
		transformSubTree(root, base);
		
		return base;
	}

	private static Element asElement(ModelTag tag) {
		Element el = new Element("tag");
		el.addAttribute(new Attribute("id", String.valueOf(tag.getTagId())));
		el.addAttribute(new Attribute("name", tag.getTagName()));
		el.addAttribute(new Attribute("dotted-name", tag.getTagDotted()));
		return el;
	}
	
	private static void transformSubTree(ModelTag current, Element base) {

		logger.debug("do subtree for :" + current);
		Element mine = asElement(current);
		
		base.appendChild(mine);
		
		logger.debug(current.getTagName());
		for (ModelTag m: current.getChildren()) {
			transformSubTree(m, mine);
		}
		
	}		
	
	public static void output(Document doc) {
		try {

			logger.debug("starting");
			FileOutputStream out = new FileOutputStream("c:\\output.xml");
			Serializer ser = new Serializer(out, "ISO-8859-1");
			ser.setIndent(2);
			ser.write(doc);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
